package com.oa.ylms.apps.actions.goods;

import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.goods.IGoodsCategoryService;
import com.oa.ylms.apps.business.goods.IGoodsInfoService;
import com.oa.ylms.apps.business.goods.IGoodsOrderService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.IOrderStates;
import com.oa.ylms.apps.constants.goods.IGoodsInfoStates;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.goods.GoodCategoryValue;
import com.oa.ylms.apps.entity.goods.GoodInfoValue;
import com.oa.ylms.apps.entity.goods.GoodOrderDetailValue;
import com.oa.ylms.apps.entity.goods.GoodOrderValue;

@SuppressWarnings("serial")
public class GoodsOrderAction extends BasicAction {

	private IGoodsInfoService infoService;
	private IGoodsCategoryService cateService;
	private IGoodsOrderService orderService;
	private IStaffInfoService staffService;

	private GoodOrderValue goodOrder;
	private GoodOrderDetailValue orderDetail;
	private List<GoodCategoryValue> cateList;
	private List<GoodInfoValue> infoList1;
	private List<GoodOrderValue> list;
	private List<GoodOrderDetailValue> detailList;
	private List<GoodOrderValue>checkingList;
	private Integer categoryId;
	private Integer orderId;
	private Integer infoId;
	private String returnTime;
	private String comments;
	private Integer submitState;
	private Integer orderState;
	private String str;
	private String items;
	private StaffInfoValue staffInfo;
	private String returnJsonString;
	private Integer states=0;

	public String execute() {
		try {
			staffInfo = (StaffInfoValue) session.get("login_staffInfo");
			list = orderService.queryPersonalOrder(staffInfo,
					states, rows, currpage);
			totalpage=orderService.countTpages(staffInfo, states, rows);
			count=orderService.getTotalNumber();
			checkingList=orderService.queryPersonalOrder(staffInfo, IOrderStates.CHECKING, rows, currpage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "myOrder";
	}

	public String queryByState() {
		try {
			list = orderService.queryPersonalOrder(staffInfo, orderState, rows,
					currpage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "queryByState";
	}

	public String toCheckOrder() {
		try {
			staffInfo = (StaffInfoValue) session.get("login_staffInfo");
			list = orderService.queryCheckOrder(staffInfo, rows, currpage);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toCheckOrder";
	}

	public String toApply() {
		try {
			cateList = cateService.queryAll(IDEDStates.ENABLE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toApply";
	}

	public String queryByCategory() {
		try {
			if (categoryId != -1) {
				infoList1 = infoService.queryAllByCateId(categoryId,
						IGoodsInfoStates.FORUSER);
				response.setContentType("text/html; charset=gb2312");
				response.setHeader("Cache-Control", "no-cache");
				this.str = getOptionsFrom(infoList1); // obj.toString();
				response.getWriter().print(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String getOptionsFrom(List<GoodInfoValue> list) {

		StringBuilder sb = new StringBuilder();

		for (GoodInfoValue giv : list) {
			String temp = "<option total=\"" + giv.getTotal() + "\" remain=\""
					+ giv.getOnStore() + "\"  value=\""
					+ giv.getGoodId().toString() + "\">" + giv.getGoodName()
					+ "</option>";

			sb.append(temp);
		}

		return sb.toString();

	}

	public String apply() {
		try {
			staffInfo = (StaffInfoValue)session.get("login_staffInfo");

			System.out.println("items:" + items);
			String[] str = items.split(";");
			for (int i = 0; i < str.length; i++) {
				String[] temp = str[i].split(",");
				String goodId = temp[0];
				String qty = temp[1];
				GoodInfoValue goodInfo = infoService.queryByGoodId(Integer
						.parseInt(goodId));
				orderDetail.setGoodInfo(goodInfo);
				orderDetail.setQuantity(Integer.parseInt(qty));
				detailList.add(orderDetail);
			}
			orderService.applyGoods(staffInfo, detailList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "apply";
	}

	public String toReturnList() {
		try {

			list = orderService.queryReturnOrder();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toReturnList";
	}

	public String toReturn() {
		try {
			goodOrder = orderService.queryById(orderId);
			detailList = orderService.queryDetailByOrderId(orderId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toReturn";
	}

	@SuppressWarnings("unchecked")
	public String back() {
		try {
			JSONArray array = JSONArray.fromObject(returnJsonString);
			for (Iterator iter = array.iterator(); iter.hasNext();) {
				JSONObject jsonObject = (JSONObject) iter.next();
				detailList.add((GoodOrderDetailValue) JSONObject.toBean(jsonObject, GoodOrderDetailValue.class));
			}
			orderService.backGoods(detailList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String toCheck() {
		try {
			staffInfo = (StaffInfoValue) session.get("login_staffInfo");
			list = orderService.queryCheckOrder(staffInfo, 10, 1);

			// goodOrder = orderService.queryById(orderId);
			// detailList = orderService.queryDetailByOrderId(orderId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toCheck";
	}

	public String check() {

		try {
			staffInfo = (StaffInfoValue) session.get("login_staffInfo");
			if (submitState == 1) {
				orderService.checkPass(staffInfo, orderId, comments);
			}
			if (submitState == 0) {
				orderService.checkUnpass(staffInfo, orderId, comments);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "check";
	}

	public String queryCheckOrderByState() {
		try {
			list = orderService.queryCheckOrderByState(staffInfo, orderState, 10, 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "queryCheckOrderByState";
	}

	public IGoodsInfoService getInfoService() {
		return infoService;
	}

	public void setInfoService(IGoodsInfoService infoService) {
		this.infoService = infoService;
	}

	public IGoodsCategoryService getCateService() {
		return cateService;
	}

	public void setCateService(IGoodsCategoryService cateService) {
		this.cateService = cateService;
	}

	public IGoodsOrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(IGoodsOrderService orderService) {
		this.orderService = orderService;
	}

	public IStaffInfoService getStaffService() {
		return staffService;
	}

	public void setStaffService(IStaffInfoService staffService) {
		this.staffService = staffService;
	}

	public GoodOrderValue getGoodOrder() {
		return goodOrder;
	}

	public void setGoodOrder(GoodOrderValue goodOrder) {
		this.goodOrder = goodOrder;
	}

	public GoodOrderDetailValue getOrderDetail() {
		return orderDetail;
	}

	public void setOrderDetail(GoodOrderDetailValue orderDetail) {
		this.orderDetail = orderDetail;
	}

	public List<GoodCategoryValue> getCateList() {
		return cateList;
	}

	public void setCateList(List<GoodCategoryValue> cateList) {
		this.cateList = cateList;
	}

	public List<GoodInfoValue> getInfoList1() {
		return infoList1;
	}

	public void setInfoList1(List<GoodInfoValue> infoList1) {
		this.infoList1 = infoList1;
	}

	public List<GoodOrderValue> getList() {
		return list;
	}

	public void setList(List<GoodOrderValue> list) {
		this.list = list;
	}

	public List<GoodOrderDetailValue> getDetailList() {
		return detailList;
	}

	public void setDetailList(List<GoodOrderDetailValue> detailList) {
		this.detailList = detailList;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public Integer getOrderId() {
		return orderId;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	public Integer getInfoId() {
		return infoId;
	}

	public void setInfoId(Integer infoId) {
		this.infoId = infoId;
	}

	public String getReturnTime() {
		return returnTime;
	}

	public void setReturnTime(String returnTime) {
		this.returnTime = returnTime;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public Integer getSubmitState() {
		return submitState;
	}

	public void setSubmitState(Integer submitState) {
		this.submitState = submitState;
	}

	public Integer getOrderState() {
		return orderState;
	}

	public void setOrderState(Integer orderState) {
		this.orderState = orderState;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public String getItems() {
		return items;
	}

	public void setItems(String items) {
		this.items = items;
	}

	public String getReturnJsonString() {
		return returnJsonString;
	}

	public void setReturnJsonString(String returnJsonString) {
		this.returnJsonString = returnJsonString;
	}

	public List<GoodOrderValue> getCheckingList() {
		return checkingList;
	}

	public void setCheckingList(List<GoodOrderValue> checkingList) {
		this.checkingList = checkingList;
	}

	public Integer getStates() {
		return states;
	}

	public void setStates(Integer states) {
		this.states = states;
	}

}
